package com.hd.trans.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ProgressBar
import android.widget.TextView
import androidx.core.view.isVisible
import com.hd.trans.R
import com.hd.trans.binding.setOnDebouncedClickListener
import com.hd.trans.databinding.TransDlgOfflinePkgProgressBinding
import com.hd.trans.db.bean.HuDunLanguage
import com.hd.trans.framework.dialog.CstDialog

class OfflinePkgProgressDialog(context: Context) : CstDialog<TransDlgOfflinePkgProgressBinding>(context, LayoutInflater.from(context).inflate(
    R.layout.trans_dlg_offline_pkg_progress, null), false){

    private val langNameTextViewList = arrayListOf<TextView>()
    private val progressTextViewList = arrayListOf<TextView>()
    private val progressBarList = arrayListOf<ProgressBar>()

    private val langLayoutList = arrayListOf<ViewGroup>()

    private val mLanguages = arrayListOf<HuDunLanguage>()

    init {
        langLayoutList.add(mDataBinding.llLang1)
        langLayoutList.add(mDataBinding.llLang2)

        langNameTextViewList.add(mDataBinding.tvLang1)
        langNameTextViewList.add(mDataBinding.tvLang2)

        progressTextViewList.add(mDataBinding.tvProgress1)
        progressTextViewList.add(mDataBinding.tvProgress2)

        progressBarList.add(mDataBinding.progressBar1)
        progressBarList.add(mDataBinding.progressBar2)

        mDataBinding.btnClose.setOnDebouncedClickListener {
            dismiss()
        }
    }

    fun setLanguages(languages:List<HuDunLanguage>) {
        langLayoutList.forEach {
            it.isVisible = false
        }
        for (i in languages.indices){
            if (i > 1) {
                //最多显示两个语种
                break
            }
            langLayoutList[i].isVisible = true
            langNameTextViewList[i].text = languages[i].name
            progressBarList[i].progress = 0
            progressTextViewList[i].text = "0%"
        }
        mLanguages.clear()
        mLanguages.addAll(languages)
    }

    @SuppressLint("SetTextI18n")
    fun updateProgress(code: String, progress:Float) {
        var index = -1
        for (i in mLanguages.indices){
            if (mLanguages[i].translateCode == code) {
                index = i
                break
            }
        }
        if (index == -1)
            return

        progressBarList[index].progress = (progress * 100).toInt()
        val progressStr = String.format("%.2f", progress * 100)
        progressTextViewList[index].text = "${progressStr}%"

    }

}